package cz.muni.fi.cassandra.Firebrand.dao;

import static org.firebrandocm.dao.cql.QueryBuilder.allColumns;
import static org.firebrandocm.dao.cql.QueryBuilder.from;
import static org.firebrandocm.dao.cql.QueryBuilder.select;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.firebrandocm.dao.PersistenceFactory;
import org.firebrandocm.dao.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cz.muni.fi.cassandra.Firebrand.entities.Tweet;

@Repository
public class TweetDaoImpl implements TweetDao {
	
	@Autowired
	public PersistenceFactory factory;

	public void createTweet(Tweet tweet) {
		if (tweet == null){
			throw new IllegalArgumentException("tweet is null");
		}
		
		factory.persist(tweet);
		
	}

	public Tweet getTweetById(String id) {
		if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
		
		return factory.get(Tweet.class, id);
		
	}

	public void deleteTweet(Tweet tweet) {
		if (tweet == null){
			throw new IllegalArgumentException("user doesnt exist");
		}
		factory.remove(tweet);
		
	}

	public List<Tweet> getAllTweets() {
		List<Tweet> tweets = new ArrayList<Tweet>();
		tweets = factory.getResultList(Tweet.class, Query.get(select(allColumns(), from(Tweet.class))));
		return Collections.unmodifiableList(tweets);
	}

}
